home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1999 / MacHack 1999.toast / The Hacks / AltiVec Effect / EffectFilter32.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-06-25  |  5.2 KB  |  179 lines  |  [TEXT/CWIE]

  1. #include "BltMacros.h"
  2.  
  3. // --------------------------------------------------------------------------------------
  4.  
  5. //
  6. //    Code that draws our actual effect.  Note that this code is very slow. 
  7. //  It is intended only as an example/placeholder for code you will write.
  8. //  A good first optimization would be to replace the floating-point math
  9. //  with fixed point.
  10. //    
  11. //    32 bit case. For explanation of what this function does, see the
  12. // comments in the 16 bit case, above.
  13. //
  14. short isPressed(unsigned short k );
  15. const short key_option = 0x3a;
  16. const short key_shift  = 0x38;
  17.  
  18. static void EffectFilter32(BlitGlobals *glob);
  19. static void EffectFilter32(BlitGlobals *glob)
  20. {
  21.     if (isPressed (key_option)) {
  22.         long height = glob->height;
  23.         long *srcA = glob->sources[0].srcBaseAddr;
  24.         //vector unsigned char srcA = *(vector unsigned char*)glob->sources[0].srcBaseAddr;
  25.         long *dst = glob->dstBaseAddr;
  26.         long srcABump = glob->sources[0].srcRowBytes - (glob->width * 4);
  27.         long dstRowBytes = glob->dstRowBytes;
  28.         long* dstBaseAddr = glob->dstBaseAddr;
  29.         long srcRowBytes = glob->sources[0].srcRowBytes;
  30.         long* srcBaseAddr = glob->sources[0].srcBaseAddr;
  31.         UInt8* puiRandoms = glob->puiRandoms;
  32.         UInt8* puiEndRandoms = puiRandoms + 32768;
  33.         UInt8* puiBrightnessRoll = glob->puiBrightnessRoll;
  34.         UInt8* puiEndBrightness = glob->puiBREnd;
  35.         
  36.         puiRandoms += ((Random () * 16000) / 65535) + 16000;
  37.  
  38.         while (height--)
  39.         {
  40.             vector unsigned char lowPart, hiPart, Align;
  41.             vector unsigned char srcAVec;
  42.             vector unsigned char randomVec;
  43.             vector unsigned char dstVec;
  44.             vector unsigned char brightnessVec;
  45.             
  46.             long width = glob->width;
  47.             
  48.             srcA = (void *) (((Ptr) srcBaseAddr) + (srcRowBytes * (glob->height - height - 1)));
  49.             srcA += (*puiRandoms/4);
  50.             dst = (void*)(((Ptr)dstBaseAddr) + ((glob->height - height - 1) * dstRowBytes));
  51.             width -= (long)(*puiRandoms/4);
  52.             //dst += (long)(*puiRandoms/4);
  53.             
  54.             brightnessVec = vec_ld (0, puiBrightnessRoll);
  55.             brightnessVec = vec_splat (brightnessVec, 0);
  56.             
  57.             //brightnessVec = *(vector unsigned char*)puiBrightnessRoll;
  58.             puiBrightnessRoll++;
  59.             if (puiBrightnessRoll > puiEndBrightness) {
  60.                 puiBrightnessRoll = glob->puiBRStart;
  61.             }
  62.             
  63.             // Account for wide vectors
  64.             width /= 4;
  65.             
  66.             while (width--)
  67.             {
  68.                 // Get the source into a vector
  69.                 lowPart = vec_ld (0, (UInt8*)srcA);
  70.                 hiPart = vec_ld (16, (UInt8*)srcA);
  71.                 Align = vec_lvsl (0, (UInt8*)srcA);
  72.                 srcAVec = vec_perm (lowPart, hiPart, Align);
  73.                 srcA += 4;
  74.                 
  75.                 // Get the random bits into a vector and add them to the source
  76.                 randomVec = vec_ld (0, (UInt8*)puiRandoms);
  77.                 dstVec = vec_adds (srcAVec, randomVec);
  78.                 //dstVec = srcAVec;
  79.                 puiRandoms+=16;
  80.                 if (puiRandoms > puiEndRandoms) {
  81.                     puiRandoms = glob->puiRandoms;
  82.                 }
  83.                 
  84.                 // Add in the brightness
  85.                 dstVec = vec_adds (dstVec, brightnessVec);
  86.                 vec_st (dstVec, 0, (UInt8*)dst);
  87.                 dst += 4;
  88.             }
  89.         }
  90.     } else {
  91.         long height = glob->height;
  92.         long *srcA = glob->sources[0].srcBaseAddr;
  93.         long *dst = glob->dstBaseAddr;
  94.         long srcABump = glob->sources[0].srcRowBytes - (glob->width * 4);
  95.         long dstRowBytes = glob->dstRowBytes;
  96.         long* dstBaseAddr = glob->dstBaseAddr;
  97.         long srcRowBytes = glob->sources[0].srcRowBytes;
  98.         long* srcBaseAddr = glob->sources[0].srcBaseAddr;
  99.         UInt8* puiRandoms = glob->puiRandoms;
  100.         UInt8* puiEndRandoms = puiRandoms + 32768;
  101.         UInt8* puiBrightnessRoll = glob->puiBrightnessRoll;
  102.         UInt8* puiEndBrightness = glob->puiBREnd;
  103.         
  104.         puiRandoms += ((Random () * 16000) / 65535) + 16000;
  105.  
  106.         while (height--)
  107.         {            
  108.             long width = glob->width;
  109.             
  110.             srcA = (void *) (((Ptr) srcBaseAddr) + (srcRowBytes * (glob->height - height - 1)));
  111.             dst = (void*)(((Ptr)dstBaseAddr) + ((glob->height - height - 1) * dstRowBytes));
  112.             width -= (long)(*puiRandoms/4);
  113.             dst += (long)(*puiRandoms/4);
  114.             puiRandoms++;
  115.             if (puiRandoms > puiEndRandoms) {
  116.                 puiRandoms = glob->puiRandoms;
  117.             }
  118.  
  119.             puiBrightnessRoll++;
  120.             if (puiBrightnessRoll > puiEndBrightness) {
  121.                 puiBrightnessRoll = glob->puiBRStart;
  122.             }
  123.             
  124.             while (width--)
  125.             {
  126.                 UInt32     thePixelValue;
  127.                 UInt16    pixels[4];
  128.                 UInt16    uiRandom;
  129.                 
  130.                 thePixelValue = Get32(srcA);
  131.                 srcA++;
  132.                 
  133.                 puiRandoms++;
  134.                 if (puiRandoms > puiEndRandoms) {
  135.                     puiRandoms = glob->puiRandoms;
  136.                 }
  137.                  uiRandom = (UInt16)*puiRandoms;
  138.                 
  139.                 pixels[0] = thePixelValue >> 24;
  140.                 pixels[1] = ((thePixelValue >> 16) & 0xff) + (UInt16)uiRandom;
  141.                 pixels[2] = ((thePixelValue >> 8) & 0xff) + (UInt16)uiRandom;
  142.                 pixels[3] = ((thePixelValue >> 0) & 0xff) + (UInt16)uiRandom;
  143.                 
  144.                 pixels [ 1 ] = (float)pixels [ 1 ] + *puiBrightnessRoll;
  145.                 pixels [ 2 ] = (float)pixels [ 2 ] + *puiBrightnessRoll;
  146.                 pixels [ 3 ] = (float)pixels [ 3 ] + *puiBrightnessRoll;
  147.                 
  148.                 if (pixels [ 1 ] > 255) {
  149.                     pixels [ 1 ] = 255;
  150.                 }
  151.                 
  152.                 if (pixels [ 2 ] > 255) {
  153.                     pixels [ 2 ] = 255;
  154.                 }
  155.                 
  156.                 if (pixels [ 3 ] > 255) {
  157.                     pixels [ 3 ] = 255;
  158.                 }
  159.  
  160.                 thePixelValue = (pixels[0] << 24) | (pixels[1] << 16) | (pixels[2] << 8) | (pixels[3]);
  161.                     
  162.                 Set32(dst,thePixelValue);
  163.                 dst++;
  164.                 
  165.             }
  166.         }
  167.     }
  168.     
  169. } // DrawEffectFrameEffect32Bit
  170.  
  171. short isPressed(unsigned short k )
  172. // k =  any keyboard scan code, 0-127
  173. {
  174.     unsigned char km[16];
  175.  
  176.     GetKeys( (unsigned long *) km);
  177.     return ( ( km[k>>3] >> (k & 7) ) & 1);
  178. }
  179.